/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2017-05-31 14:34:18
 * @Description: 非业务逻辑数据获取领域相关业务逻辑公共
 */

import * as API from '../api'
import globalMixin from './globalMixin'
import language from '../i18n/en'

var storeDataPartMixin = {

    mixins: [globalMixin],

    methods: {
        storeDataPartMixin_GetCountryList(callback) {
            let tthis = this,
                data = JSON.parse(Unit.getLocalStorage('ms_country_list')) || {};
            if (_.isEmpty(data)) {
                API.get_country_list({
                    success: function(result) {
                        Unit.setLocalStorage('ms_country_list', JSON.stringify(result));
                        if (callback) {
                            callback(result);
                        }
                    }
                });
            } else {
                if (callback) {
                    callback(data);
                }
            }
        },

        storeDataPartMixin_GetStateList(country_id, callback) {
            let dataObj = Unit.getLocalStorage("ms_state_list");
            if (dataObj == null || dataObj == undefined || dataObj == '') {
                dataObj = {};
            } else {
                dataObj = JSON.parse(dataObj);
            }
            let data = dataObj[country_id];

            if (_.isEmpty(data)) {
                API.get_state_list({
                    data: country_id,
                    success: function(response) {
                        if (callback) {
                            response.unshift({
                                value: '',
                                label: 'Anywhere'
                            });
                            callback(response);
                        }
                        dataObj[country_id] = response;
                        Unit.setLocalStorage('ms_state_list', JSON.stringify(dataObj));
                    }
                })
            } else {
                if (callback) {
                    callback(data);
                }
            }
        },

        getSelectLable: function(key, val, defaultStr) {
            let select = JSON.parse(Unit.getLocalStorage(key));
            for (var i in select) {
                if (select[i].value == val) {
                    return select[i].label;
                }
                if (key == 'ms_height' && (val == '-47' || val == '')) {
                    return (select[i]) ? select[i].label : this.language.myProfile_notToSay;
                }
            }
            return defaultStr || this.language.unify_pleaseAskMe;
        },

        getLocalStorageMultipleSelectLable: function(key, val, spe, len, defaultStr) {
            let select = JSON.parse(Unit.getLocalStorage(key)),
                str = this.getMultipleSelectLable(select, val, spe, len);
            return str || defaultStr || this.language.unify_pleaseAskMe;
        },

        getMultipleSelectLable: function(select, val, spe, len) {
            let arr = [],
                str = '';
            spe = spe ? spe : ',';
            if (select.length > 0) {
                for (var i in select) {
                    if (parseInt(parseInt(val) & parseInt(select[i].value)) > 0) {
                        arr.push(select[i].label);
                    }
                }
                str = arr.join(spe);
                if (len) {
                    str = Unit.subString(str, len);
                }
            }
            return str;
        },

        getMultipleSelectValue: function(arr) {
            if (!((typeof arr == 'object') && arr.constructor == Array)) {
                return arr || 0;
            }
            var value = 0;
            for (var i in arr) {
                if (arr[i]) {
                    value += parseInt(arr[i]);
                }
            }
            if (value == 0) {
                value = '';
            }
            return value;
        },

        getMultipleSelectArr: function(value, select) {
            let arr = [];
            if (value) {
                for (var i in select) {
                    if (parseInt(parseInt(value) & parseInt(select[i].value)) > 0) {
                        arr.push(select[i].value);
                    }
                }
            }
            return arr;
        },

        userInforStruct() {
            return {
                user: false,
                im: false,
                userInfo: {
                    about: {},
                    account: {},
                    approved_private_album_count: 0,
                    detail: {
                        basics: {},
                        lifestyle: {},
                        personalinfo: {},
                        worth: {},
                    },
                    is_test_user: 0,
                    last_request_private_album_time: '',
                    luxuries_pictures: [],
                    match: {},
                    not_accepting_flirting: '0',
                    photo: {},
                    pictures: [],
                    private_album_request_num: 0,
                    private_pic_num: 0,
                    private_pictures: [],
                    status: {}
                }
            }
        },

        getUserObj(uid) {
            let usersInfo = Unit.getObjSessionStorage('usersInfo') || {};
            if (!_.isEmpty(usersInfo) && !_.isEmpty(usersInfo[uid])) {
                return usersInfo[uid];
            }
            return {};
        },

        setUsersObj(uid, val) {
            let usersInfo = Unit.getObjSessionStorage('usersInfo') || {};
            usersInfo[uid] = val;
            Unit.setSessionStorage('usersInfo', JSON.stringify(usersInfo));
        }

    }

}

export default storeDataPartMixin;
